package exam.test11.q2;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class PersonRepositoryTest {

    SequenceGenerator sequenceGenerator;
    PersonRepository personRepository;

    @Before
    public void before() {
        sequenceGenerator = new SequenceGenerator();
        personRepository = new PersonRepository();
        personRepository.setSequenceGenerator(sequenceGenerator);
    }

    // Implement your test cases
    // Hint: The java.lang.Integer.intValue() is an inbuilt method in Java that returns the value of an Integer as an int.

    // ########## YOUR CODE STARTS HERE ##########

    @Test(timeout = 1000)
    public void testSave() {
        Person personOne = new Person("Join", 10, "Student", false, false);
        personRepository.save(personOne);
        assertTrue(personOne.getId()!=0);

        Person personTwo = new Person("Jim", 12, "Student", false, false);
        personTwo.setId(2);
        boolean exception = false;
        try {
            personRepository.save(personTwo);
        } catch (Exception e) {
            exception = true;
        }
        assertTrue("An exception should be thrown", exception);


        Person personThree = new Person("Lily", 11, "Student", false, false);
        personRepository.save(personThree);
    }

    @Test(timeout = 1000)
    public void testFindById() {
        Person personOne = new Person("Join", 10, "Student", false, false);
        personRepository.save(personOne);
        Person personTwo = new Person("Jim", 12, "Student", false, false);
        personRepository.save(personTwo);
        Person personThree = new Person("Lily", 11, "Student", false, false);
        personRepository.save(personThree);

        assertEquals(personRepository.findById(personOne.getId()), personOne);
        assertEquals(personRepository.findById(personTwo.getId()), personTwo);
        assertEquals(personRepository.findById(personThree.getId()), personThree);

        assertNull(personRepository.findById(0));
        assertNull(personRepository.findById(-1));
        assertNull(personRepository.findById(10));
    }

    // ########## YOUR CODE ENDS HERE ##########


}
